package com.rimi.dao.impl;

import com.rimi.bean.Page;
import com.rimi.bean.User;
import com.rimi.dao.UserDao;
import com.rimi.util.JdbcUtil;
import com.rimi.util.StringUtils;

import java.util.List;

/**
 * @author chengliang
 */
public class UserDaoImpl extends JdbcUtil implements UserDao {

    @Override
    public User selectUserByLoginName(String loginName) {
        return executeQueryObject(User.class, "select * from user_tb where user_login_name = ?", loginName);
    }

    @Override
    public List<User> selectUserListBy(User queryUser, Page page) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from user_tb where 1=1  ");
        //动态拼接查询条件(动态SQL)
        if(queryUser != null){
            if(!StringUtils.isEmpty(queryUser.getUserLoginName())){
                sql.append(" and user_login_name like '%"+queryUser.getUserLoginName()+"%'");
            }
            if(!StringUtils.isEmpty(queryUser.getUserName())){
                sql.append(" and user_name like '%"+queryUser.getUserName()+"%'");
            }
            if(queryUser.getRoleId() != null && queryUser.getRoleId() > 0){
                sql.append(" and role_id = "+queryUser.getRoleId());
            }
        }
        //分页条件
        sql.append("  limit "+page.getStartLine()+","+page.getPageSize());
        System.out.println(sql.toString());
        return executeQueryList(User.class,sql.toString());
    }

    @Override
    public boolean updateEnable(int userId, int userEnable) {
        return executeUpdateSql("update user_tb set user_enable = ? where user_id = ?",userEnable,userId);
    }

    @Override
    public boolean deleteBy(String[] stringIds) {
        StringBuffer sql = new StringBuffer();
        sql.append(" delete from user_tb where user_id  in (");
        for (String stringId : stringIds) {
            sql.append("?,");
        }
        return executeUpdateSql(sql.toString().substring(0,sql.length()-1)+")",stringIds);
    }

    @Override
    public boolean insertUser(User user) {
        String sql = "insert into user_tb value(0,?,?,?,?,?,?,?)";
        return executeUpdateSql(sql,user.getUserName(),user.getUserLoginName()
                ,user.getUserPassword()
                ,user.getUserCreateTime(),user.getUserAvatar(),user.getRoleId()
                ,user.getUserEnable()==0?0:1);
    }

    @Override
    public User selectUserById(String id) {
        return executeQueryObject(User.class,"select * from user_tb where user_id = ?",id);
    }

    @Override
    public boolean updateUser(User user) {
        String sql = "update user_tb set user_name = ? ,user_login_name = ?,user_password = ?" +
                ",user_create_time = ?,user_avatar = ?,role_id = ?,user_enable = ? " +
                "where user_id = ?";

        return executeUpdateSql(sql,user.getUserName(),user.getUserLoginName()
                ,user.getUserPassword()
                ,user.getUserCreateTime(),user.getUserAvatar(),user.getRoleId()
                ,user.getUserEnable()==0?0:1,user.getUserId());
    }


    @Override
    public int countUserBy(User queryUser) {
        StringBuffer sql = new StringBuffer();
        sql.append("select count(0) from user_tb where 1=1");
        //动态拼接查询条件(动态SQL)
        if(queryUser != null){
            if(!StringUtils.isEmpty(queryUser.getUserLoginName())){
                sql.append(" and user_login_name like '%"+queryUser.getUserLoginName()+"%'");
            }
            if(!StringUtils.isEmpty(queryUser.getUserName())){
                sql.append(" and user_name like '%"+queryUser.getUserName()+"%'");
            }
            if(queryUser.getRoleId() != null && queryUser.getRoleId() > 0){
                sql.append(" and role_id = "+queryUser.getRoleId());
            }
        }
        System.out.println(sql.toString());
        return executeQueryCount(sql.toString());
    }

    @Override
    public List<User> selectUserListByIds(String ids) {
        return executeQueryList(User.class,"select * from user_tb where user_id in("+ids+")");
    }

    @Override
    public void saveUser(List<User> list) {
        if(list == null || list.size() <= 0){
            return;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("insert into user_tb(user_name,user_login_name,user_password) values ");
        for (User user : list) {
            sql.append("('");
            sql.append(user.getUserName()).append("','");
            sql.append(user.getUserLoginName()).append("',");
            sql.append("'123456'");
            sql.append("),");
        }
        //最后去掉一个,
        String sqlString = sql.toString();
        executeUpdateSql(sqlString.substring(0,sqlString.length()-1));
    }
}
